﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Xml;
using System.Reflection;
using System.IO;

namespace Schüleranmeldung.Mail
{
    class MailParser
    {
        public Dictionary<string, object> ParseMail(MailMessage mail)
        {
            return ParseMail(mail.Body);
        }

        public Dictionary<string, object> ParseMail(string mail)
        {
            MappingHelper mapping = new MappingHelper();
            mapping.LoadMappingTable();
            Dictionary<string, object> objects = new Dictionary<string, object>();
            if (!String.IsNullOrWhiteSpace(mail))
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(mail);
                foreach (XmlNode node in doc.SelectSingleNode("Datensatz").ChildNodes)
                {
                    MappingInfo info = mapping.GetMappingInfo(node.Name);
                    if (info != null)
                    {
                        Type t = Type.GetType("Schüleranmeldung.Database." + info.Table);
                        object o;
                        if (objects.ContainsKey(info.Table))
                        {
                            o = objects[info.Table];
                        }
                        else
                        {
                            o = Activator.CreateInstance(t);
                            objects.Add(info.Table, o);
                        }

                        PropertyInfo pi = t.GetProperty(info.ColumnName);
                        if (pi != null)
                        {
                            if (!String.IsNullOrWhiteSpace(node.InnerText))
                            {
                                object value;
                                if (pi.PropertyType == typeof(DateTime?))
                                {
                                    DateTime? dt = new DateTime();
                                    if (node.InnerText.Length == 4)
                                    {
                                        DateTime year = new DateTime(2007, 1, 1);
                                        dt = year;
                                    }
                                    else
                                    {
                                        dt = DateTime.Parse(node.InnerText);
                                    }
                                    value = dt;
                                }
                                else if (pi.PropertyType == typeof(Int16?))
                                {
                                    Int16? i = new Int16();
                                    //i = Int16.Parse(node.InnerText);
                                    value = i;
                                }
                                else if (pi.PropertyType == typeof(Int32?))
                                {
                                    Int32? i = new Int32();
                                    i = Int32.Parse(node.InnerText);
                                    value = i;
                                }
                                else if (pi.PropertyType != typeof(String))
                                {
                                    value = pi.PropertyType.GetMethod("Parse", new Type[] { typeof(string) }).Invoke(null, new object[] { node.InnerText });
                                }
                                else
                                {
                                    value = node.InnerText;
                                }
                                pi.SetValue(o, value, null);
                            }
                        }
                    }
                }
            }
            return objects;
        }
    }
}